package com.foreknow.exer7;

import com.foreknow.oopTest.order;

import java.util.Scanner;

/**
 * @author shkstart
 * @create 2021-07-27 8:03
 */
public class Test {
    /**
     * 输出图书信息
     *
     * @param books
     */
    public void printBooksInfo(Book[] books) {
        books[0] = new Book(1, "java", 33.45, 50);
        books[1] = new Book(2, "jsp", 55.45, 150);
        books[2] = new Book(3, "ssm", 77.45, 250);
        for (int i = 0; i < books.length; i++) {
            System.out.println(books[i].getBid() + "\t" + books[i].getBname() + "\t" + books[i].getPrice() + "\t" + books[i].getStorage());
        }
    }

    /**
     * 购买图书
     *
     * @param books
     * @return
     */
    public Order buyBook(Book[] books) {
        Order order = new Order("0001");
        for (int i = 0; i < 3; i++) {
            System.out.println("请输入购买图书编号");
            Scanner sc = new Scanner(System.in);
            int pid = sc.nextInt();
            System.out.println("请输入购买数量");
            int pnum = sc.nextInt();
            OrderItem item = new OrderItem(books[pid - 1].getBname(),
                    books[pid - 1].getPrice(), pnum);
            order.setItem(item, i);
            System.out.println("请继续购买图书");
        }
        return order;
    }

    public static void main(String[] args) {
        Test t = new Test();
        Book[] books = new Book[3];
        // 1.输出图书信息
        t.printBooksInfo(books);

        // 2.购买图书
        books[0] = new Book(1, "java", 33.45, 50);
        books[1] = new Book(2, "jsp", 55.45, 150);
        books[2] = new Book(3, "ssm", 77.45, 250);

        Order order = t.buyBook(books);

        // 3.输出订单信息
        // 3.1输出订单的编号
        System.out.println(order.getOrderId());
        // 3.2输出订单的总额
        System.out.println("订单的总额为" + order.getTotal());

        // 3.3输出商品列表中的内容
        OrderItem[] itt = order.getItems();
        for (int i = 0; i < itt.length; i++) {
            OrderItem io = itt[i];
            System.out.println(io.getPname() + "--" + io.getPprice() + "--" + io.getNum());
        }
    }

}
